<?php
/**
 * Class that operate on table 'sic_partidas'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2014-10-07 23:30
 */
class SicPartidasMySqlDAO implements SicPartidasDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return SicPartidasMySql 
	 */
	public function load($sicTransaccionIdTransaccion, $sicCuentasIdCuenta){
		$sql = 'SELECT * FROM sic_partidas WHERE sic_transaccion_id_transaccion = ?  AND sic_cuentas_id_cuenta = ? ';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($sicTransaccionIdTransaccion);
		$sqlQuery->setNumber($sicCuentasIdCuenta);

		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM sic_partidas';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM sic_partidas ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param sicPartida primary key
 	 */
	public function delete($sicTransaccionIdTransaccion, $sicCuentasIdCuenta){
		$sql = 'DELETE FROM sic_partidas WHERE sic_transaccion_id_transaccion = ?  AND sic_cuentas_id_cuenta = ? ';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($sicTransaccionIdTransaccion);
		$sqlQuery->setNumber($sicCuentasIdCuenta);

		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param SicPartidasMySql sicPartida
 	 */
	public function insert($sicPartida){
		$sql = 'INSERT INTO sic_partidas (carga_partida, abono_partida, iva, sic_transaccion_id_transaccion, sic_cuentas_id_cuenta) VALUES (?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($sicPartida->cargaPartida);
		$sqlQuery->set($sicPartida->abonoPartida);
		$sqlQuery->setNumber($sicPartida->iva);

		
		$sqlQuery->setNumber($sicPartida->sicTransaccionIdTransaccion);

		$sqlQuery->setNumber($sicPartida->sicCuentasIdCuenta);

		$this->executeInsert($sqlQuery);	
		//$sicPartida->id = $id;
		//return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param SicPartidasMySql sicPartida
 	 */
	public function update($sicPartida){
		$sql = 'UPDATE sic_partidas SET carga_partida = ?, abono_partida = ?, iva = ? WHERE sic_transaccion_id_transaccion = ?  AND sic_cuentas_id_cuenta = ? ';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($sicPartida->cargaPartida);
		$sqlQuery->set($sicPartida->abonoPartida);
		$sqlQuery->setNumber($sicPartida->iva);

		
		$sqlQuery->setNumber($sicPartida->sicTransaccionIdTransaccion);

		$sqlQuery->setNumber($sicPartida->sicCuentasIdCuenta);

		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM sic_partidas';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByCargaPartida($value){
		$sql = 'SELECT * FROM sic_partidas WHERE carga_partida = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByAbonoPartida($value){
		$sql = 'SELECT * FROM sic_partidas WHERE abono_partida = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByIva($value){
		$sql = 'SELECT * FROM sic_partidas WHERE iva = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByCargaPartida($value){
		$sql = 'DELETE FROM sic_partidas WHERE carga_partida = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByAbonoPartida($value){
		$sql = 'DELETE FROM sic_partidas WHERE abono_partida = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByIva($value){
		$sql = 'DELETE FROM sic_partidas WHERE iva = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return SicPartidasMySql 
	 */
	protected function readRow($row){
		$sicPartida = new SicPartida();
		
		$sicPartida->sicTransaccionIdTransaccion = $row['sic_transaccion_id_transaccion'];
		$sicPartida->sicCuentasIdCuenta = $row['sic_cuentas_id_cuenta'];
		$sicPartida->cargaPartida = $row['carga_partida'];
		$sicPartida->abonoPartida = $row['abono_partida'];
		$sicPartida->iva = $row['iva'];

		return $sicPartida;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return SicPartidasMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>